package control.sit
{
	import core.Control;
	import core.Model;
	import core.View;
	
	import events.SitEvent;
	
	import global.GameState;
	import global.GameType;
	import global.Resource;
	
	import model.sit.ISitData;
	import model.sit.ISitState;
	import model.sit.SitModel;
	
	import utils.DisplayTool;
	
	import view.sit.AbsSit;
	import view.sit.ISit;

	public class SitControl
	{
		private var sitModel:SitModel;
		private var _model:Model = Model.getInstance();
		private var sits:Array;
		private var _control:Control = Control.getInstance();
		
		public function SitControl(sitModel:SitModel)
		{
			this.sitModel = sitModel;
			addListeners();	
			initSits();
		}
		
		public function clear():void
		{
			for each (var sit:ISit in sits)
			{
				sit.clear();
			}
			
		}
		
		private function initSits():void
		{
			sits = [];
			for (var i:int=0; i<6; i++)
			{
				var sit:ISit = new AbsSit(this);
				sits.push(sit);
			}
		}
		
		private function addListeners():void
		{
			sitModel.addEventListener(SitEvent.ADD,onSitAdd);
			sitModel.addEventListener(SitEvent.REMOVE,onSitRemove);
			sitModel.addEventListener(SitEvent.STATE_CHANGE,onSitStateChange);
			sitModel.addEventListener(SitEvent.MONEY_CHANGE,onSitMoneyChange);
			sitModel.addEventListener(SitEvent.SIT_TALK,onSitTalk);
			sitModel.addEventListener(SitEvent.SIT_AUTO_STATE,onSitAutoChange);
			sitModel.addEventListener(SitEvent.SIT_ITEM_ANIMATE,onSitItemAnimate);
			sitModel.addEventListener(SitEvent.SIT_MONEY_CHANGE_ANIMATION,onSitMoneyChangeAnimation);
		}
		
		private function onSitMoneyChangeAnimation(event:SitEvent):void
		{
			var sitId:int = event.sitId;
			var sit:ISit = sits[sitId];
			if (sit)
			{
				sit.updateMoneyChangeAnimate(int(event.money));
			}
		}
		
		/*private function onSitEmoteMoneyChange(event:SitEvent):void
		{
			var sitId:int = event.sitId;
			var sit:ISit = sits[sitId];
			if (sit)
			{
				sit.updateMoneyChangeAnimate(-1);
			}
		}*/
		
		private function onSitItemAnimate(event:SitEvent):void
		{
			var sitId:int = event.sitId;
			var sit:ISit = sits[sitId];
			if (sit)
			{
				sit.updateItemAnimate(Resource.getItemAnimate(event.animateId));
			}
		}
		
		private function onSitAutoChange(event:SitEvent):void
		{
			var sitId:int = event.sitId;
			var state:int = event.autoState;
			var sit:ISit = sits[sitId];
			if (sit)
			{
				sit.updateAutoState(state);
			}
			
			if (sitId == GameState.playerCurrentSitId)
			{
				GameState.isAutoPlay = Boolean(state);
				_control.playPanelControl.onAutoServer();;
				//GameState.isAutoPlay = !GameState.isAutoPlay;
			}
		}
		
		private function onSitTalk(event:SitEvent):void
		{
			var sitId:int = event.sitId;
			var content:String = event.content;
			var sit:ISit = sits[sitId];
			if (sit)
			{
				sit.updateTalk(content.split(":")[1]);
			}
		}
		
		public function onSitHeadClick(sitId:int):void
		{
			_control.addFriendPanelControl.open(sitId);
		}
		
		public function onSitHeadOver(sitId:int):void
		{
			_control.sitInfoPanelControl.open(sitId);
		}
		
		public function onSitHeadOut():void
		{
			_control.sitInfoPanelControl.close();
		}
		
		private function onSitMoneyChange(event:SitEvent):void
		{
			var sitId:int = event.sitId;
			var money:String = event.money;
			var sit:ISit = sits[sitId];
			if (sit)
			{
				sit.updateMoney(money);
			}
			
		}
		
		private function onSitStateChange(event:SitEvent):void
		{
			var sitState:ISitState = event.sitState;
			var sit:ISit = sits[sitState.sitId];
			if (sit)
			{
				sit.sitId = sitState.sitId;
				sit.updateHoldCards(sitState.holdCount);
				sit.updateAction(getAction(sitState.action));
				sit.updateTotalTime(sitState.totalTime);
				sit.updateLeftSecond(sitState.leftSecond);
				
				//不出，清除上一次出牌
				if (sitState.sitId == GameState.playerCurrentSitId)
				{
					if (sitState.action == GameType.SIT_STATE_PLAYING)
					{
						//提示玩家出牌
						_control.playPanelControl.hintPlayerToPlay(true);
						_control.playPanelControl.autoPlay();
						
					}
					else 
					{
						_control.playPanelControl.hintPlayerToPlay(false);
					}
					
					
					//更新座位按钮
					_control.playPanelControl.updatePlayState(sitState.action);
					
					
				}
				
				if (sitState.action == GameType.SIT_STATE_PLAYING)
				{
					_model.tableModel.clearSitShowCards(sitState.sitId);
					sit.showCountTimer(true);
				}
				else
				{
					//清除警报声
					_control.soundControl.clearPlayAlarm();
					sit.showCountTimer(false);
				}
				//播放名言名句
				if (sitState.totalTime > 0)
				{
					_control.famusRemarkControl.step();
				}
			}
		}
		
		public function showGiveCardAnimate(sitId:int):void
		{
			_control.tableControl.showGiveCardAnimate(sitId);
			_control.soundControl.playSound(GameType.SOUND_CARD);
		}
		
		public function sitCircleHint(sitId:int):void
		{
			if (sitId == GameState.playerCurrentSitId)
			{
				_control.playPanelControl.circleHintPlayer();
				_control.soundControl.playAlarm();
			}
		}
		
		private function getAction(type:int):String
		{
			var str:String = "";
			switch(type)
			{
				case GameType.SIT_STATE_NULL:
					
					break;
				case GameType.SIT_STATE_START_PLAY:
					str = "已出牌";
					break;
				case GameType.SIT_STATE_WAITING:
					str = "不出";
					break;
				case GameType.SIT_STATE_PLAYING:
					str = "出牌中";
					break;
				case GameType.SIT_STATE_SITTING:
					str = "游戏中";
					break;
			}
			return str;
		}
		
		private function onSitAdd(event:SitEvent):void
		{
			var sitData:ISitData = event.sitData;
			var sit:ISit = sits[sitData.id];
			if (sit)
			{
				sit.updateTitle(sitData.roleData.roleName);
				sit.updateHeadImg(sitData.roleData.imgUrl);
				sit.updateVip(sitData.roleData.vip);
				//sit.updateMoney(sitData.roleData.money);
				sit.self.x = GameState.sitXYS[sitData.id][0];
				sit.self.y = GameState.sitXYS[sitData.id][1];
				
				
				
				View.getInstance().tableLayer.addSit(sit.self);
			}
		}
		
		private function onSitRemove(event:SitEvent):void
		{
			var sitData:ISitData = event.sitData;
			var sit:ISit = sits[sitData.id];
			if (sit)
			{
				sit.clear();
				DisplayTool.removeChild(sit.self.parent,sit.self);
			}
		}
		
		
		
	}
}